#!/usr/bin/env python
import os, sys
from liwe.module import LiweModule
class Payment ( LiweModule ):
	def __init__ ( self, liwe ):
		super ( Payment, self ).__init__ ( liwe, "payment" )
		self.dbm = self.liwe.dbm
	#{{{ PAYMENT
	#{{{def set_payment ( self, id_payment = 0, module = 'payment', id_obj = 0, uid = 0, payment = 0, payment_date = , mode = '', note = '', owner = 0, created =  ):
	def set_payment ( self, id_payment = 0, module = 'payment', id_obj = 0, uid = 0, payment_value = 0, payment_date = '', mode = '', note = '' ):
		values = {
				'module' : module,
				'id_obj' : id_obj,
				'uid' : uid,
				'payment_value' : payment_value / 100.0,
				'payment_date' : payment_date,
				'mode' : mode,
				'note' : note }
		if not id_payment:
			values [ 'created' ] = '*NOW()'
			values [ 'uid' ] = self.liwe.pyhp.session.data.get ( "uid", 0 )
			self.dbm.insert ( 'payment_payment', values )
			id_payment = self.dbm.insert_id ()
		else:
			self.dbm.update ( "payment_payment", values, "id = %s" % id_payment )

		values [ 'id_payment' ] = id_payment
		return values
	#}}}
	#{{{def get_payment ( self, id_payment = 0 ):
	def get_payment ( self, id_payment = 0 ):
		if not id_payment:
			self.liwe.set_error ( '1', 'id_payment value is not valido' )
			return [];
		return self.dbm.single_row ( 'payment_payment' , '*', 'id = %s' % id_payment, debug = True )
	#}}}
	#{{{def del_payment ( self, ids = "" ):
	def del_payment ( self, ids = "" ):
		ids = ids.split ( "|" )
		str_ids = " ,".join ( ids )
		self.dbm.delete ( "payment_payment", "id IN ( %s )" % str_ids )
	#}}}
	#{{{def list_payment ( self, module = '', quant = 10, start_point = 0, do_count = 0 ):
	def list_payment ( self, module = '', quant = 10, start_point = 0, do_count = 0 ):
		conds = []
		if module: conds.append ( "module = '%s'" % module )
		extras = ''
		tot_rows = self.dbm.count ( 'payment_payment', 'id', ' AND '.join ( conds ) )
		if do_count:
			extras = " LIMIT %s,%s" % ( start_point, quant )


		resp = self.dbm.all_rows ( 'payment_payment', '*', ' AND '.join ( conds ), extras = extras )
		if not resp:
			resp = []
		return { "rows" : resp, "tot_rows" : tot_rows }
	#}}}
	#}}}
